package com.deyang.util;

import android.app.Activity;
import android.text.TextUtils;


import com.deyang.application.MyApplication;
import com.deyang.config.HttpConfig;
import com.deyang.entity.TokenInfo;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @author 王德阳
 * @data 创建时间: 2018/5/4 0004
 * @Description retrofit 请求
 */
public class RetrofitUtil {

    private static final String TAG = "RetrofitUtil";

    private static final long connectTime = 30000L;
    private static final long readTime = 30000L;
    private static Retrofit retrofit;

    /**
     * 一般创建Retrofit
     *
     * @param activity
     */
    public static Retrofit buildRetrofit(Activity activity) {
        MyApplication mApp = (MyApplication) activity.getApplication();
        //日志打印
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Logger.i(TAG, "retrofitBack = " + message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .connectTimeout(connectTime, TimeUnit.MILLISECONDS)
                .readTimeout(readTime, TimeUnit.MILLISECONDS);


        //传递请求头
        final TokenInfo tokenInfo = mApp.getTokenInfo();
        if (tokenInfo != null && !TextUtils.isEmpty(tokenInfo.getToken())) {
            okHttpBuilder.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();
                    Request request = original.newBuilder()
                            .header(HttpConfig.HEADER_TOKEN, tokenInfo.getToken())
                            .method(original.method(), original.body())
                            .build();
                    return chain.proceed(request);
                }
            });
        }

        //其他配置
        retrofit = new Retrofit.Builder()
                .baseUrl(HttpConfig.HTTP_URL)
                .client(okHttpBuilder.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                //登录 会直接返回 String       其他返回json
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;
    }

    /**
     * 登陆创建Retrofit
     */
    public static Retrofit buildLoginRetrofit() {
        //日志打印
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Logger.i(TAG, "retrofitBack = " + message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .connectTimeout(connectTime, TimeUnit.MILLISECONDS)
                .readTimeout(readTime, TimeUnit.MILLISECONDS);

        //其他配置
        retrofit = new Retrofit.Builder()
                .baseUrl(HttpConfig.HTTP_URL)
                .client(okHttpBuilder.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                //登录 会直接返回 String       其他返回json
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        return retrofit;
    }

}
